<?php
/**
* Admin Authorize.net Controller
*
* This handles all AJAX requests from the Admin GUI Authorize.net Configuration page.
* These methods will create an AJAX response containing JSON data. The JSON
* data is read by the JS code and rendered into interface.
*
* @access 	public
* @author 	Rob, Valentin
* @copyright	ResMania 2009 all rights reserved.
* @version	1.0
* @link		http://developer.resmania.com/api
* @since  	05-2009
*/
class RM_Admin_eWAYpaymentsController extends RM_Controller
{
    /**
     * This loads the Authorize.net configuration, the data is retrieved using the
     * RM_eWAYpayments class
     *
     * @return 	json    json array information containing Authorize.net information
     */
    public function configJsonAction(){

        $model = new RM_eWAYpayments;
        $settings = $model->getSettings()->toArray();

        $json = new stdClass;
        $json->settings = $settings;

        return array(
            'data' => $json
        );
    }

    /**
     * This saves the authorize.net settings from the plugin->ewaypayments setting page
     *
     * @return 	json    success value true/false
     */
    public function updateJsonAction(){

        $id = $this->_getParam('id');
        $ewayCustomerID = $this->_getParam('ewayCustomerID');      
        $test_mode = $this->_getParam('test_mode');
        $preauth_mode = $this->_getParam('preauth_mode');
        $eway_default = $this->_getParam('eway_default');
        $ccprocesstext = $this->_getParam('ccprocesstext');
        $overridetotal = $this->_getParam('overridetotal');
        $storedpaymentsURL = $this->_getParam('storedpaymentsURL');

        $model = new RM_eWAYpayments;

        // find the current record from the DB
        $details = $model->find($id)->current();

        $status = 'true'; // set this true the if we have a problem we set it false

        if (!$details) {
            $status = 'false';
        } else {

            $details->ewayCustomerID = $ewayCustomerID;            
            $details->test_mode = $test_mode;
            $details->preauth_mode = $preauth_mode;
            $details->eway_default = $eway_default;
            $details->ccprocesstext = $ccprocesstext;
            $details->overridetotal = $overridetotal;
            $details->storedpaymentsURL = $storedpaymentsURL;
            $result = $details->save();

            if ($result!='1') $status = 'false';
        }
        
        return array(
            'data' => array('success' => $status)
        );

    }
}